package com.example.demo.service.impl;
import com.example.demo.Utils.TimeUtils;
import com.example.demo.Utils.UUIDUtils;
import com.example.demo.bean.Drink;
import com.example.demo.bean.ResultPageVO;
import com.example.demo.bean.ResultVO;
import com.example.demo.mapper.CaculateMapper;
import com.example.demo.mapper.DrinkMapper;
import com.example.demo.requestentity.DrinkDelete;
import com.example.demo.requestentity.DrinkInsert;
import com.example.demo.requestentity.DrinkRequsetVO;
import com.example.demo.requestentity.DrinkUpdate;
import com.example.demo.requestentity.OrdersInsertRecords;
import com.example.demo.service.DrinkService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Repository
public class DrinkServiceImpl implements DrinkService {

    @Resource
    public DrinkMapper drinkMapper;

    public void add(Drink drink) {
        drink.setId(UUIDUtils.getUUID());
        drink.setCreateDate(TimeUtils.getNowTime());
        drinkMapper.add(drink);
    }

    public void delete(Drink drink) {
        drinkMapper.delete(drink);
    }

    public void update(Drink drink) {
        drink.setUpdateDate(TimeUtils.getNowTime());
        drinkMapper.update(drink);
    }

    public ResultPageVO<Drink> query(String drinkName,Integer pageSize,Integer currentPage) {
        PageHelper.startPage(currentPage,pageSize);
        List<Drink> query = drinkMapper.query(drinkName);
        for(int i=1;i<=query.size();i++){
            query.get(i-1).setId(String.valueOf(i));
        }
        return new ResultPageVO<>(new PageInfo<>(query));
    }

    public List<Drink> queryDrinkName(){
        return drinkMapper.queryDrinkName();
    }

    private boolean verifyInsert(List<DrinkInsert> drinkInsert){

        for(DrinkInsert insert:drinkInsert){

        }
        return false;
    }

    public ResultVO operation(DrinkRequsetVO drinkRequsetVO){
        int insertNums=0;
        int updateNums=0;
        int deleteNums=0;
        String date = TimeUtils.getNowTime();
        List<DrinkInsert>  drinkInserts= drinkRequsetVO.getInsertRecords();
        if(drinkInserts!=null&&drinkInserts.size()>0 ) {
            Drink drink=new Drink();
            drink.setCreateDate(date);
            drink.setId(UUIDUtils.getUUID());
            drink.setDrinkname(drinkInserts.get(0).getDrinkName());
            drink.setPrice(drinkInserts.get(0).getPrice());
            insertNums=drinkMapper.add(drink);
        }
        List<DrinkUpdate>  drinkUpdates= drinkRequsetVO.getUpdateRecords();
        if(drinkUpdates!=null&&drinkUpdates.size()>0 ) {
            Drink drink=new Drink();
            drink.setDrinkname(drinkUpdates.get(0).getDrinkName());
            drink.setPrice(drinkUpdates.get(0).getPrice());
            updateNums=drinkMapper.update(drink);
        }
        List<DrinkDelete>  drinkDeletes= drinkRequsetVO.getRemoveRecords();
        if(drinkDeletes!=null&&drinkDeletes.size()>0 ) {
            Drink drink=new Drink();
            drink.setDrinkname(drinkDeletes.get(0).getDrinkName());
            deleteNums=drinkMapper.delete(drink);
        }
        if(updateNums>=0&&insertNums>=0&&deleteNums>=0){
            return  ResultVO.success(insertNums,deleteNums,updateNums);
        }
        return ResultVO.error("请求失败");
    }
}
